What is @opentelemetry/context-zone-peer-dep?
@opentelemetry/context-zone-peer-dep is a package that provides context management capabilities using Zone.js. It is part of the OpenTelemetry project, which is a set of APIs, libraries, agents, and instrumentation to provide observability for applications.
What are @opentelemetry/context-zone-peer-dep's main functionalities?
Context Management
This feature allows you to manage context using Zone.js. The code sample demonstrates how to set the global context manager to use ZoneContextManager, create a new context, and run a function within that context.
const { context, setGlobalContextManager } = require('@opentelemetry/api');
const { ZoneContextManager } = require('@opentelemetry/context-zone-peer-dep');
// Set the global context manager to use ZoneContextManager
setGlobalContextManager(new ZoneContextManager());
// Create a new context
const ctx = context.active().setValue('key', 'value');
// Run a function within the context
context.with(ctx, () => {
console.log(context.active().getValue('key')); // Outputs: 'value'
});
Other packages similar to @opentelemetry/context-zone-peer-dep
@opentelemetry/context-async-hooks
@opentelemetry/context-async-hooks provides context management using Node.js async_hooks. It is similar to @opentelemetry/context-zone-peer-dep but is designed for use in Node.js environments, leveraging the async_hooks API for context propagation.
zone.js
zone.js is a library that provides execution context tracking for JavaScript. It is the underlying library used by @opentelemetry/context-zone-peer-dep for context management. While zone.js is more general-purpose, @opentelemetry/context-zone-peer-dep integrates it specifically for OpenTelemetry context management.
OpenTelemetry Context Zone Peer Dependency
This module provides Zone Context Manager with a peer dependency for zone-js for Web applications.
If you use Angular you already have the zone-js and you should use this package.
If you don't have your own zone-js please use @opentelemetry/context-zone
Installation
npm install --save @opentelemetry/context-zone-peer-dep
Usage
import { ConsoleSpanExporter, SimpleSpanProcessor } from '@opentelemetry/tracing';
import { WebTracer } from '@opentelemetry/web';
import { ZoneContextManager } from '@opentelemetry/context-zone-peer-dep';
const webTracerWithZone = new WebTracer({
contextManager: new ZoneContextManager()
});
webTracerWithZone.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
const span1 = webTracerWithZone.startSpan('foo1');
webTracerWithZone.withSpan(span1, () => {
console.log('Current span is span1', webTracerWithZone.getCurrentSpan() === span1);
setTimeout(() => {
const span2 = webTracerWithZone.startSpan('foo2');
console.log('Current span is span1', webTracerWithZone.getCurrentSpan() === span1);
webTracerWithZone.withSpan(span2, () => {
console.log('Current span is span2', webTracerWithZone.getCurrentSpan() === span2);
setTimeout(() => {
console.log('Current span is span2', webTracerWithZone.getCurrentSpan() === span2);
}, 500);
});
console.log('Current span is span2', webTracerWithZone.getCurrentSpan() === span2);
}, 500);
console.log('Current span is span1', webTracerWithZone.getCurrentSpan() === span1);
});
Useful links
License
Apache 2.0 - See LICENSE for more information.